home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CreatingGames / GameCreators / TADS / manuals / TEX / UNIQIND.C < prev   
Encoding:
C/C++ Source or Header  |  1992-11-13  |  3.1 KB  |  139 lines

  1. /* Copyright (c) 1992 by Michael J. Roberts.  All Rights Reserved. */
  2. /*
  3. Name
  4.   uniqind.c - clean up generated index by combining repeated entries
  5. Function
  6.   Takes a sorted index file (generated by a TeX file such as tads5.tex),
  7.   and combines repeated index lines into single index entries (with
  8.   comma-separated page lists).  Also groups runs of each initial letter
  9.   by inserting \medskip between each letter run.
  10.   
  11.   Reads standard input, writes standard output.
  12. Notes
  13.   Some hand-editing is probably still required after this pass!
  14. Modified
  15.   09/07/92 MJRoberts     - creation
  16. */
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20.  
  21. main()
  22. {
  23.     char  buf[128];
  24.     char  prv[128];
  25.     char  key[50];
  26.     char  out[128];
  27.     char *outp = out;
  28.     char *p;
  29.     char  letter;
  30.     char  newlet;
  31.     int   pagelist[50];
  32.     int   pagecnt = 0;
  33.     int   i;
  34.     int   j;
  35.     int   cur;
  36.     
  37.     for (;;)
  38.     {
  39.     if (!gets(buf)) break;
  40.     
  41.     p = strstr(buf, " !0 ");
  42.     if (!p)
  43.     {
  44.         fprintf(stderr, "bad line: \"%s\"\n", buf);
  45.     }
  46.     else
  47.     {
  48.         /* get the current key */
  49.         memcpy(key, buf, (size_t)(p - buf));
  50.         key[p - buf] = '\0';
  51.         
  52.         /* if there's no old key, or this key differs, start new line */
  53.         if (outp == out
  54.         || (!(strlen(prv) == p - buf - 1
  55.               && isalpha(key[0])
  56.               && key[p - buf - 1] == 's'
  57.               && !memicmp(prv, key, (size_t)(p - buf - 1)))
  58.             && stricmp(key, prv)))
  59.         {
  60.         /* if there's a line pending, write it */
  61.         if (outp != out)
  62.         {
  63.             newlet = (isupper(out[0]) ? out[0] : toupper(out[0]));
  64.             if (isalpha(out[0]) && newlet != letter)
  65.             puts("\n\\medskip\n");
  66.             
  67.             for (i = 0 ; i < pagecnt ; ++i)
  68.             {
  69.             if (i)
  70.             {
  71.                 *outp++ = ',';
  72.                 *outp++ = ' ';
  73.             }
  74.             sprintf(outp, "%d", pagelist[i]);
  75.             outp += strlen(outp);
  76.             }
  77.             *outp++ = '.';
  78.             *outp = '\0';
  79.             puts(out);
  80.             outp = out;
  81.             letter = (isupper(out[0]) ? out[0] : toupper(out[0]));
  82.         }
  83.         
  84.         /* save the new key, and start building the line */
  85.         strcpy(prv, key);
  86.         
  87.         if (!isalpha(key[0]))
  88.             sprintf(out, "{\\tt %s}, ", key);
  89.         else
  90.             sprintf(out, "%s, ", key);
  91.  
  92.         /* make first entry in page list */
  93.         pagecnt = 1;
  94.         pagelist[0] = atoi(p + 4);
  95.         outp += strlen(out);
  96.         }
  97.         else
  98.         {
  99.         /* it's a match - simply add new page number */
  100.         cur = atoi(p + 4);
  101.         for (i = 0 ; i < pagecnt ; ++i)
  102.         {
  103.             if (pagelist[i] == cur) goto do_not_add;
  104.             if (pagelist[i] > cur) break;
  105.         }
  106.         for (j = i ; j < pagecnt ; ++j)
  107.             pagelist[j+1] = pagelist[j];
  108.         pagelist[i] = cur;
  109.         ++pagecnt;
  110.         do_not_add: ;
  111.         }
  112.     }
  113.     }
  114.  
  115.     /* if there's a line pending, write it */
  116.     if (outp != out)
  117.     {
  118.     newlet = (isupper(out[0]) ? out[0] : toupper(out[0]));
  119.     if (isalpha(out[0]) && newlet != letter)
  120.         puts("\n\\medskip\n");
  121.  
  122.     for (i = 0 ; i < pagecnt ; ++i)
  123.     {
  124.         if (i)
  125.         {
  126.         *outp++ = ',';
  127.         *outp++ = ' ';
  128.         }
  129.         sprintf(outp, "%d", pagelist[i]);
  130.         outp += strlen(outp);
  131.     }
  132.     *outp++ = '.';
  133.     *outp = '\0';
  134.  
  135.     puts(out);
  136.     outp = out;
  137.     }
  138. }
  139.